using System;
using System.Collections.Generic;
using System.Text;

namespace DocLibApp
{
    /// <summary>
    /// This class is used to setup individual document library data file
    /// </summary>
    class doc_dbsetup
    {
        private SQLiteDB myDB = null;
        private string DBFileName = "";

        public doc_dbsetup(string NewDBFileName)
        {
            this.DBFileName = NewDBFileName;

            myDB = new SQLiteDB(this.DBFileName);

        }

        public string SetupDatabase()
        {
            string res = "";

            myDB.OpenConnection();

            res += CreateTables();

            myDB.CloseConnection();

            return res;
        }

        /// <summary>
        /// create the essential system tables for document library
        /// </summary>
        /// <returns></returns>
        private string CreateTables()
        {
            string res = "";

            //Document main table
            res += myDB.ExecuteSQL(def.tb_doc_sql_create) + System.Environment.NewLine;
            //User table
            res += myDB.ExecuteSQL(def.tb_user_sql_create) + System.Environment.NewLine;
            //Document audit table
            res += myDB.ExecuteSQL(def.tb_doc_audit_sql_create) + System.Environment.NewLine;
            //Document tag table
            res += myDB.ExecuteSQL(def.tb_doc_tag_sql_create) + System.Environment.NewLine;
            //Document template header table
            res += myDB.ExecuteSQL(def.tb_template_header_sql_create) + System.Environment.NewLine;
            //Document template fields table
            res += myDB.ExecuteSQL(def.tb_template_field_sql_create) + System.Environment.NewLine;
            
            //**** System Tables ***//
            //System Code
            res += myDB.ExecuteSQL(def.tb_System_Code_sql_create) + System.Environment.NewLine;
            //System table field
            res += myDB.ExecuteSQL(def.tb_table_columns_sql_create) + System.Environment.NewLine;
            //Sysem Log table
            res += myDB.ExecuteSQL(def.tb_System_Log_sql_create) + System.Environment.NewLine;
            //System List Table
            res += myDB.ExecuteSQL(def.tb_list_sql_create) + System.Environment.NewLine;
            //System Tag table
            res += myDB.ExecuteSQL(def.tb_System_Tag_sql_create) + System.Environment.NewLine;

            //Security
            res += myDB.ExecuteSQL(def.tb_sec_role_header_sql_create) + System.Environment.NewLine;
            res += myDB.ExecuteSQL(def.tb_sec_role_entry_sql_create) + System.Environment.NewLine;
            res += myDB.ExecuteSQL(def.tb_sec_role_assignment_sql_create) + System.Environment.NewLine;
            return res;
        }
    }
}
